// package_shop/pages/product/product.js
import Api from '../../../utils/http/api'
import common_utils from '../../../utils/common_utils'

const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    product: null,
    options: {},
    count: 1,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getProduct(options.id)
    this.setData({
      options: common_utils.getRedirectUrl(options)
    })
  },

  onShareAppMessage: function () {
    return {
      title: '快来购买品质鲜生吧～',
      imageUrl: this.data.product.picture,
      path: '/package_shop/pages/product/product' + this.data.options
    }
  },
  //展示分享弹窗
  onShowPopup() {
    this.selectComponent('#sPopup').onShowPopup()
  },
  //关闭分享弹窗
  onClosePopup() {
    this.selectComponent('#sPopup').onHidePopup()
  },
  //关闭海报弹窗
  onCloseHBPopup() {
    this.selectComponent('#myPopupCanvas').onHidePopup()
  },
  //展示购买弹窗
  onShowBuyPopup() {
    this.selectComponent('#buyPopup').onShowPopup()
  },
  //关闭购买弹窗
  onCloseBuyPopup() {
    this.selectComponent('#buyPopup').onHidePopup()
  },
  onBuy() {
    this.onShowBuyPopup()
  },
  onCounterChange(e) {
    let count = e.detail.count
    this.setData({
      count
    })

  },
  quickBuy() {
    wx.navigateTo({
      url: '/package_shop/pages/product-order/product-order?product=' + JSON.stringify(this.data.product) + '&count=' + this.data.count,
    })
  },
  // 生成海报
  onCanvastoImg() {
    this.onClosePopup()
    this.selectComponent('#myPopupCanvas').onShowPopup()
  },
  onSaveImg(e) {
    this.imagePath = e.detail.path;
    this.setData({
      image: this.imagePath,
    });
    if (this.isSave) {
      this.saveImage(this.imagePath);
    }
  },

  saveImage() {
    if (this.imagePath && typeof this.imagePath === 'string') {
      this.isSave = false;
      wx.saveImageToPhotosAlbum({
        filePath: this.imagePath,
      });
      wx.showToast({
        title: '保存成功',
      })

      this.selectComponent('#myPopupCanvas').onHidePopup()
    }
  },
  getProduct(id) {
    let params = {
      id: id
    }
    Api.getProduct(params).then(res => {
      let product = res.data
      product.picture = common_utils.getApiImg(product.picture)
      product.mainPicture = common_utils.getApiImg(product.mainPicture)


      this.setData({
        product,

      })


    }).catch(err => console.log(err))
  }

})